package git.soulbgm.controller;

import git.soulbgm.common.pojo.ResultData;
import git.soulbgm.common.pojo.ReturnCode;
import git.soulbgm.config.ShiroConfig;
import git.soulbgm.pojo.User;
import git.soulbgm.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author SoulBGM
 * @version 1.0
 * @date 2019/10/12 16:03
 */
@RestController
@Api(tags = "home", description = "登录")
public class LoginController {

    @ApiOperation("登录")
    @RequestMapping(path = "login", method = RequestMethod.POST)
    public ResultData login(@RequestParam String username, @RequestParam String password) throws NoSuchFieldException {
        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        if (subject.isAuthenticated()) {
            // 已经登录了
            return ResultData.getResultData(ReturnCode.HAS_LOGGED, principal);
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        ReturnCode code = null;
        try{
            subject.login(token);
        } catch (UnknownAccountException uae) {
            // 未知账户
            code = ReturnCode.UNKNOWN_ACCOUNT;
        } catch (IncorrectCredentialsException ice) {
            // 密码不正确
            code = ReturnCode.USERNAME_OR_PASSWORD_IS_INCORRECT;
        } catch (LockedAccountException lae) {
            // 账户已锁定
            code = ReturnCode.ACCOUNT_LOCKED;
        } catch (ExcessiveAttemptsException eae) {
            // 用户名或密码错误次数过多
            code = ReturnCode.TOO_MANY_INCORRECT_USERNAME_OR_PASSWORD;
        } catch (AuthenticationException e) {
            // 用户名或密码不正确
            code = ReturnCode.USERNAME_OR_PASSWORD_IS_INCORRECT;
        }
        if (!subject.isAuthenticated()) {
            // 登录失败
            token.clear();
            code = code != null ? code : ReturnCode.LOGIN_FAIL;
        } else {
            return ResultData.getResultData(ReturnCode.LOGIN_SUCCESS, subject.getPrincipal());
        }
        return ResultData.getResultData(code);
    }

    @ApiOperation("注销")
    @RequestMapping(path = "logout", method = RequestMethod.POST)
    public ResultData logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout();
            return ResultData.getResultData(ReturnCode.LOGOUT_SUCCESS);
        } else {
            return ResultData.getResultData(ReturnCode.UNKNOWN_ACCOUNT);
        }
    }

    @Autowired
    private UserController userController;

    @ApiOperation("注册")
    @RequestMapping(path = "registered", method = RequestMethod.POST)
    public ResultData registered(@RequestBody User user) {
        if (user.getUsername() != null && user.getPassword() != null) {
            String salt = user.getUsername() + "+" + StringUtil.getRandomString(6);
            String encryption = new SimpleHash(ShiroConfig.HASH_ALGORITHM_NAME, user.getPassword(), salt, ShiroConfig.HASH_ITERATIONS).toString();
            user.setSalt(salt);
            user.setPassword(encryption);
            return userController.save(user);
        }
        return ResultData.getResultData(ReturnCode.WRONG_FORMAT);
    }

    @RequestMapping(path = "notRole", method = RequestMethod.GET)
    public ResultData notRole() {
        return ResultData.getResultData(ReturnCode.NO_SUCH_PERMISSION);
    }

//    @RequiresPermissions("/user/findOne")
    @RequestMapping("findOne")
    public String findOne() {
        return "skfsdjkf";
    }

}
